﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.ZhiJian
{
    public partial class 产品出库单2 : System.Web.UI.Page
    {
        private string chanpinlist = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            int hetongid = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (Request.QueryString["HTID"] != null)
                {
                    int.TryParse(Request.QueryString["HTID"], out hetongid);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText =
@"select
产品出库单.合同ID,
产品销售合同视图.产品ID,
产品出库单.日期,
产品销售合同视图.订货单位,
产品销售合同视图.合同编号,
产品出库单.序号,
产品销售合同视图.品名,
产品销售合同视图.规格,
产品出库单.生产单位ID,
产品出库单.总件数,
产品出库单.总支数,
产品出库单.总重量,
产品出库单.总长度,
产品销售合同视图.运输方式,
产品出库单.备注,
产品出库单.车号,
产品出库单.仓储主管,
产品出库单.库管员
from 产品出库单,产品销售合同视图
where 产品出库单.合同ID=产品销售合同视图.ID
and 产品出库单.ID=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.CangChuZhuGuan.DataBind();
                        string temp = ((string)Reader["仓储主管"]).Trim();
                        if (this.CangChuZhuGuan.Items.FindByValue(temp) != null)
                        {
                            this.CangChuZhuGuan.Text = temp;
                            this.CangChuZhuGuan0.Text = this.CangChuZhuGuan.SelectedItem.Text;
                        }

                        this.ViewState["BillID"] = BillID;
                        this.RiQi.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                        this.DingHuo.Text = (string)Reader["订货单位"];
                        this.HeTongHao.Text = ((string)Reader["合同编号"]).Trim();
                        this.DanHao.Text = ((string)Reader["序号"]).Trim();
                        this.ChanPin.Text = (string)Reader["品名"];
                        this.GuiGe.Text = (string)Reader["规格"];
                        this.ShengChan.Text = ((int)Reader["生产单位ID"]).ToString();
                        this.ShengChan0.Text = this.ShengChan.SelectedItem.Text;


                        this.ZongJian.Text = ((int)Reader["总件数"]).ToString();
                        this.ZongZhiShu.Text = ((int)Reader["总支数"]).ToString();
                        this.ZongZhong.Text = ((double)Reader["总重量"]).ToString();
                        this.ZongChang.Text = ((double)Reader["总长度"]).ToString();

                        this.FaHuoFangShi.Text = (string)Reader["运输方式"];
                        this.BeiZhu.Text = (string)Reader["备注"];
                        this.CheHao.Text = ((string)Reader["车号"]).Trim();
                        this.KuGuanYuan.Text = ((string)Reader["库管员"]).Trim();
                        this.ViewState["HeTongID"] = (int)Reader["合同ID"];
                        this.ViewState["ChanPinID"] = (int)Reader["产品ID"];

                        Reader.Close();
                        Cmd.CommandText =
@"select
产品库存.ID as 库存ID,
产品库存.原料供货ID,
原料供货单位.名称 as 原料供货,
产品库存.炉号,
产品库存.捆号,
产品库存.重量,
产品库存.长度
from 产品库存,原料供货单位
where 产品库存.原料供货ID=原料供货单位.ID
and 产品库存.状态=2
and 产品库存.产品类别ID=" + this.ViewState["ChanPinID"] + " and 产品库存.生产单位ID=" + this.ShengChan.Text + " and 产品库存.出库单ID=" + BillID;

                        Reader = Cmd.ExecuteReader();
                        for (int i = 1; i <= 28; i++)
                        {
                            if (Reader.Read())
                            {
                                ((TextBox)this.FindControl("GongHuo" + i)).Text = (string)Reader["原料供货"];
                                ((TextBox)this.FindControl("LuHao" + i)).Text = ((string)Reader["炉号"]).Trim();
                                ((TextBox)this.FindControl("KunHao" + i)).Text = ((string)Reader["捆号"]).Trim();
                                ((TextBox)this.FindControl("KunZhong" + i)).Text = ((double)Reader["重量"]).ToString();
                                ((TextBox)this.FindControl("KunChang" + i)).Text = ((double)Reader["长度"]).ToString();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        this.ViewState["BillID"] = -1;
                        this.Message.Text = "·出库单不存在";

                    }
                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else if (hetongid > 0)//录入新入库单
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                    this.KuGuanYuan.Text = (string)Session["OperatorName"];
                    this.ViewState["BillID"] = -1;
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText =
@"select
产品订货单位.名称 as 订货单位,
产品销售合同.合同编号,
产品.ID as 产品ID,
产品.品名,
产品.规格,
产品销售合同.运输方式
from 产品销售合同,产品订货单位,产品
where 产品销售合同.订货单位ID=产品订货单位.ID
and 产品销售合同.产品ID=产品.ID
and 产品销售合同.ID="
                    + hetongid;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.DingHuo.Text = (string)Reader["订货单位"];
                        this.HeTongHao.Text = ((string)Reader["合同编号"]).Trim();
                        this.ChanPin.Text = (string)Reader["品名"];
                        this.GuiGe.Text = (string)Reader["规格"];
                        this.FaHuoFangShi.Text = ((string)Reader["运输方式"]).Trim();
                        this.ViewState["HeTongID"] = hetongid;
                        this.ViewState["ChanPinID"] = (int)Reader["产品ID"];
                    }

                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();

                }
                else
                {
                    this.Message.Text = "·合同号不存在";

                }
            }


        }///////////

        private string CheckInput()
        {
            //int ZZhi = 0;//总支数
            double zzhong = 0;//总重量
            int zjian = 0;//总件数
            int zjian0 = 0;
            int zzhi = 0;//总指数
            double zchang = 0;//总长度
            this.Message.Text = "";

            string kunhaolist = "";
            string kunhao = "";
            int i;
            for (i = 1; i <= 28; i++)
            {
                ((TextBox)this.FindControl("GongHuo" + i)).Text = "";
                ((TextBox)this.FindControl("LuHao" + i)).Text = "";
                ((TextBox)this.FindControl("KunZhong" + i)).Text = "";
                ((TextBox)this.FindControl("KunChang" + i)).Text = "";
                this.ViewState["GongHuoID" + i] = 0;
            }
            for (i = 1; i <= 28; i++)
            {
                kunhao = ((TextBox)this.FindControl("KunHao" + i)).Text = ((TextBox)this.FindControl("KunHao" + i)).Text.Trim();
                if (kunhao != "")
                {
                    if (kunhao.IndexOf('\'') > -1)
                    {
                        return "捆编号： " + kunhao + " 包含无效字符";
                    }

                    if (kunhaolist != "")
                    {
                        kunhaolist += ",";
                    }
                    kunhaolist += "'" + kunhao + "'";
                    zjian0++;

                }
            }

            if (kunhaolist == "")
            {
                return "请录入捆号";
            }

            int billid = (int)this.ViewState["BillID"];
            string term;
            if (billid == -1)
            {
                term = " and 产品库存.状态=1";
            }
            else
            {
                term = " and (产品库存.状态=1 or 产品库存.出库单ID=" + billid + ")";
            }
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText =
@"select
产品库存.ID as 库存ID,
产品库存.原料供货ID,
原料供货单位.名称 as 原料供货,
产品库存.炉号,
产品库存.捆号,
产品库存.重量,
产品库存.长度,
产品库存.支数
from 产品库存,原料供货单位
where 产品库存.原料供货ID=原料供货单位.ID"
        + term + " and 产品库存.产品类别ID=" + this.ViewState["ChanPinID"] + " and 产品库存.生产单位ID=" + this.ShengChan.Text + " and 产品库存.捆号 in(" + kunhaolist + ")";

            //Response.Write(Cmd.CommandText);
            //Response.End();
            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();
            while (Reader.Read())
            {
                for (i = 1; i <= 28; i++)
                {
                    kunhao = ((TextBox)this.FindControl("KunHao" + i)).Text;
                    if (kunhao == ((string)Reader["捆号"]).Trim())
                    {
                        zzhi += (byte)Reader["支数"];
                        zzhong += (double)Reader["重量"];
                        zjian++;
                        zchang += (double)Reader["长度"];
                        ((TextBox)this.FindControl("GongHuo" + i)).Text = (string)Reader["原料供货"];
                        ((TextBox)this.FindControl("LuHao" + i)).Text = (string)Reader["炉号"];
                        ((TextBox)this.FindControl("KunZhong" + i)).Text = ((double)Reader["重量"]).ToString();
                        ((TextBox)this.FindControl("KunChang" + i)).Text = ((double)Reader["长度"]).ToString();
                        if (chanpinlist != "")
                        {
                            chanpinlist += ",";
                        }
                        chanpinlist += "'" + (int)Reader["库存ID"] + "'";
                        break;
                    }
                }
            }

            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();



            this.ZongZhiShu.Text = zzhi.ToString();
            this.ZongZhong.Text = (zzhong / 1000).ToString();
            this.ZongJian.Text = zjian.ToString();
            this.ZongChang.Text = zchang.ToString();
            if (zjian0 != zjian)
            {
                return "您输入的捆号有的查询不到相关信息，请检查后重新输入";
            }
            return null;
        }

        protected void GaiZhang_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedItem.Text == "True")
            {
                this.SqlDataSource1.UpdateCommand = "update [产品出库单] set [是否合格]=0 WHERE ([ID] = @ID)";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update [产品出库单] set [是否合格]=1 WHERE ([ID] = @ID)";
            }
            this.SqlDataSource1.Update();
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            if (ChanPin.Text == "油管")
            {
                Response.Redirect("质保书管理.aspx?id=" + Request.QueryString["type"]);
            }
            else
            {
                Response.Redirect("质保书管理.aspx?id=" + Request.QueryString["type"]);
            }
        }

    }
}
